﻿import com.dinmax.flash.gsa.games.gameSlot.*
import com.dinmax.flash.gsa.sgs.bridge.slot.*
import com.dinmax.flash.gsa.sgs.*
import com.dinmax.flash.gsa.sgs.game.*

class GameSlot
implements GameSlotInterface {
	
	private var serial:String;
	private var bridge:SLOTBridge;
	private var listener:SlotInterface;
	private var thisGame:Game;
	private var _result:Result;
	private var bet:Bet;
	
	private static var instance:GameSlot;
	
	public static function newInstance(bridge:SLOTBridge, serial:String):Void {
		instance= new GameSlot(bridge, serial);
	}; //fin función

	public static function getActive():GameSlot{
		return instance;
	};
	
	function GameSlot (bridge:SLOTBridge, serial:String){
		this.serial=serial;
		this.bridge=bridge;
		bet=new Bet(0);
		listener=SGS.RegisterGame(this);
		
	};	
	
	public function demo():Void{

	};
	
	public function init(result:Result):Void {
		//trace("GameSlot.as | init ");
		thisGame=listener.getActiveGame();
		bridge.GAMESLOT_2_SWF_setPosition(result.getParameters(), true);
		setTokens(listener.getPlayerCredits());
	}; //fin función
	
	public function gameLogOut():Void {
		listener.gameLogOut();
	}; //fin función
	
	public function result(result:Result):Void{
		bridge.GAMESLOT_2_SWF_setPosition(result.getParameters());
		this._result=result;
		//bridge.GAMESLOT_2_SWF_
	};
	
	public function setRules(rules:Rules):Void {
		//trace("GameSlot.as | setRules ");
		bridge.GAMESLOT_2_SWF_setRules(rules);
	}; //fin función
	
	public function play():Void {
		//trace("GameSlot.as | play tokens="+tokens);
		if(bet.getTokens() < thisGame.getMinBet()){
			bridge.GAMESLOT_2_SWF_noMinBet();
		} else {
			listener.play(bet);
			
			listener.setPlayerCredits(bet.getTokens()*thisGame.getToken(),
			listener.getPlayerCredits()-bet.getTokens()*thisGame.getToken());
		}
		bet= new Bet(0);
	}; //fin función

	public function setTokens(credits:Number):Void {
		bridge.GAMESLOT_2_SWF_setTokens(credits/thisGame.getToken());
	}; //fin función
	
	public function roundEnd(win:Boolean):Void {
		//bridge.GAMESLOT_2_SWF_setTokens(listener.getPlayerCredits()/thisGame.getToken());
		
		if(_result.getPrize().getTokenMultiply() > 0 && !win){
			bridge.GAMESLOT_2_SWF_win(_result.getPrize().getLevel());
		} else {
			listener.roundEnd();
		};

	}; //fin función

	public function addBet(tokens:Number, arg:String):Void {
		
		if(arg == undefined){
			var nBet=bet.getTokens()+tokens;
		} else if (arg == "min") {
			var nBet=thisGame.getMinBet();
		} else if (arg == "max") {
			var nBet=thisGame.getMaxBet();
		};

		if(nBet <= thisGame.getMaxBet()){
			bet.setTokens(nBet);

			listener.setPlayerCredits(bet.getTokens()*thisGame.getToken(),
			listener.getPlayerCredits()-bet.getTokens()*thisGame.getToken());

			bridge.GAMESLOT_2_SWF_setBet(bet.getTokens());
			bridge.GAMESLOT_2_SWF_setTokens(listener.getPlayerCredits()/thisGame.getToken()-nBet);
		}
		
	}; //fin función

	public function deleteBet():Void {
		bet = new Bet(0);
		bridge.GAMESLOT_2_SWF_setBet(bet.getTokens());
		listener.setPlayerCredits(bet.getTokens(), listener.getPlayerCredits());
		bridge.GAMESLOT_2_SWF_setTokens(listener.getPlayerCredits()/thisGame.getToken());
	
	}; //fin función
	
	
	
	

}; // fin de clase